﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary
{
    [Serializable]
    public class Link
    {
        [Serializable]
        public class EndPoint
        {
            public string Id { get; set; }
            public int PortId { get; set; }

            protected bool Equals(EndPoint other)
            {
                return string.Equals(Id, other.Id) && PortId == other.PortId;
            }

            public override int GetHashCode()
            {
                unchecked
                {
                    return ((Id != null ? Id.GetHashCode() : 0)*397) ^ PortId;
                }
            }

            public override bool Equals(object obj)
            {
                if (ReferenceEquals(null, obj)) return false;
                if (ReferenceEquals(this, obj)) return true;
                if (obj.GetType() != this.GetType()) return false;
                return Equals((EndPoint) obj);
            }
        }

        public EndPoint EndPoint1 { get; set; }
        public EndPoint EndPoint2 { get; set; }

        public EndPoint Try(string id, int portId)
        {
            EndPoint egressEndPoint = null;
            var endPoint = new EndPoint() {Id = id, PortId = portId};
            
            if (endPoint.Equals(EndPoint1))
            {
                egressEndPoint = EndPoint2;
            }
            else if (endPoint.Equals(EndPoint2))
            {
                egressEndPoint = EndPoint1;
            }
            return egressEndPoint;
        }
    }
}
